﻿using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Fast.Extensions.DatabaseAccessor.EntityFrameworkCore
{
    /// <summary>
    /// 删除仓储实现
    /// </summary>
    public partial class RootRepository<TEntity>
        where TEntity : class, IRootEntity, new()
    {

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual void Delete(TEntity entity)
        {
            Entities.Remove(entity);
        }
        /// <summary>
        /// 删除一条记录并返回跟踪信息的实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>代理中的实体</returns>
        public virtual EntityEntry<TEntity> DeleteWithReturnEntity(TEntity entity)
        {
            return Entities.Remove(entity);
        }
        /// <summary>
        /// 删除多条记录
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual void Delete(IEnumerable<TEntity> entities)
        {
            Entities.RemoveRange(entities);
        }
        /// <summary>
        /// 删除一条记录并立即提交
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual void DeleteAndCommit(TEntity entity)
        {
            this.Delete(entity);

            this.SaveChanges();
        }
        /// <summary>
        /// 删除一条记录并立即提交，返回跟踪信息的实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>代理中的实体</returns>
        public virtual EntityEntry<TEntity> DeleteAndCommitWithReturnEntity(TEntity entity)
        {
            var entityEntry = this.DeleteWithReturnEntity(entity);

            this.SaveChanges();

            return entityEntry;
        }
        /// <summary>
        /// 删除多条记录并立即提交
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual void DeleteAndCommit(IEnumerable<TEntity> entities)
        {
            this.Delete(entities);

            this.SaveChanges();
        }
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual async Task DeleteAsync(TEntity entity)
        {
            await Task.Run(() => this.Delete(entity));
        }
        /// <summary>
        /// 删除一条记录并返回跟踪信息的实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>代理中的实体</returns>
        public virtual async Task<EntityEntry<TEntity>> DeleteWithReturnEntityAsync(TEntity entity)
        {
            return await Task.Run(() => this.DeleteWithReturnEntity(entity));
        }
        /// <summary>
        /// 删除多条记录
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual async Task DeleteAsync(IEnumerable<TEntity> entities)
        {
            await Task.Run(() => this.Delete(entities));
        }
        /// <summary>
        /// 删除一条记录并立即提交
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual async Task DeleteAndCommitAsync(TEntity entity, CancellationToken cancellationToken = default)
        {
            await this.DeleteAsync(entity);

            await this.SaveChangesAsync(cancellationToken);
        }
        /// <summary>
        /// 删除一条记录并立即提交，返回跟踪信息的实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>代理中的实体</returns>
        public virtual async Task<EntityEntry<TEntity>> DeleteAndCommitWithReturnEntityAsync(TEntity entity, CancellationToken cancellationToken = default)
        {
            var entityEntry = await this.DeleteWithReturnEntityAsync(entity);

            await this.SaveChangesAsync(cancellationToken);

            return entityEntry;
        }
        /// <summary>
        /// 删除多条记录并立即提交
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual async Task DeleteAndCommitAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
        {
            await this.DeleteAsync(entities);

            await this.SaveChangesAsync();
        }
    }
}
